package com.lanou.sfu.songforu.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lanou.sfu.songforu.R;
import com.lanou.sfu.songforu.entity.RadioEntity;
import com.lanou.sfu.songforu.view.AnimationImageView;

import java.util.List;

import it.sephiroth.android.library.picasso.Picasso;

/**
 * Created by dllo on 15/10/29.
 */
public class RadioAdapter extends BaseAdapter{
    private List<RadioEntity.ResultEntity.ListEntity> datas;
    private Context mContext;
    private RadioEntity.ResultEntity.ListEntity string;

    public RadioAdapter(List<RadioEntity.ResultEntity.ListEntity> lists, Context context) {
        datas = lists;
        mContext = context;
    }

    @Override
    public int getCount() {
        return datas != null && datas.size() > 0 ? datas.size() : 0;
    }

    @Override
    public RadioEntity.ResultEntity.ListEntity getItem(int position) {
        if (datas != null && datas.size() > 0){
            return datas.get(position);
        } else {
            return null;
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        string = datas.get(position);
        ViewHolder holder = null;
        if (convertView == null){
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.fragment_radio_gridview_item, null);
            holder.num = (TextView) convertView.findViewById(R.id.radio_gridView_item_num);
            holder.title = (TextView) convertView.findViewById(R.id.radio_gridView_item_name);
            holder.pic = (ImageView) convertView.findViewById(R.id.radio_gridView_item_pic);
            holder.animation = (AnimationImageView) convertView.findViewById(R.id.animation);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
            RadioEntity.ResultEntity.ListEntity listEntity = getItem(position);
        if (listEntity != null) {
            holder.title.setText(listEntity.getName());
            Log.i("ddd", "  " + listEntity.getListen_num());
            holder.num.setText(listEntity.getListen_num());
            Picasso.with(mContext).load(listEntity.getThumb()).placeholder(R.mipmap.default_title_pic).into(holder.pic);
            holder.animation.loadAnimation(R.drawable.anim_attack);
            if (string.getType() == 1){
                holder.animation.setVisibility(View.VISIBLE);
            } else {
                holder.animation.setVisibility(View.GONE);
            }
        }
        return convertView;
    }

    public class ViewHolder{
        private TextView num,title;
        private ImageView pic;
        private AnimationImageView animation;
    }
}
